<template>
  <context-menu
    :content="message"
    :menu-list="ChatImageOrFileMenu"
    @select="(event, MenuItem) => MenuItem.click(event, message)"
  >
    <div class="fileMessage" @click="openLocalFile(message.localPath || null)">
      <n-avatar :size="50" :src="GetFileTypeIcon(message.fileName || '')" />
      <div class="flex-1 flex flex-col justify-center message-center gap-1">
        <p class="max-w-[200px] text-[13px] line-clamp-2">
          {{ message.fileName }}
        </p>
        <p class="text-[10px]">
          {{ FormatFileSize(message.fileSize) }}
        </p>
      </div>
      <!-- 上传进度 -->
      <div v-if="message.upload" class="upload-overlay">
        <n-progress
          :height="2"
          :percentage="message.progress || 0"
          :show-indicator="false"
          style="width: 120px"
          type="line"
        />
        <div class="upload-text">上传中 {{ message.progress }}%</div>
      </div>
      <!-- 模态下载 -->
      <div v-if="IsLocalFile(message)" class="download">
        <component :is="RenderIcon(CloudDownloadOutline, 'text-3xl text-white')"></component>
        <div class="text-white text-xs mt-4 text-center pl-4">点击下载</div>
      </div>
    </div>
  </context-menu>
</template>

<script lang="ts" setup>
import { IMessage } from "@/types/chat";
import { FormatFileSize, GetFileTypeIcon } from "@/utils/file";
import { RenderIcon } from "@/utils/common";

import { NAvatar, NProgress } from "naive-ui";
import ContextMenu from "@/components/ContextMenu/index.vue";
import { CloudDownloadOutline } from "@vicons/ionicons5";
import { useUser } from "@/store/modules/user";
import { computed } from "vue";
import { useChatMenu } from "@/hooks/useMenu.ts";

const user = useUser();
const { ChatImageOrFileMenu } = useChatMenu();
withDefaults(
  defineProps<{
    message: IMessage;
  }>(),
  {},
);
const IsLocalFile = computed(() => {
  return (message: IMessage) => message.sendId != user.userId && !message.localPath;
});

// 打开上传的本地文件
function openLocalFile(path: string | null = null) {
  if (!path) return;
  window.electronApi.openFileByPath(path);
}
</script>

<style lang="scss" scoped>
.fileMessage {
  position: relative;
  max-width: 300px;
  min-width: 220px;
  display: grid;
  grid-template-columns: 50px 1fr;
  gap: 10px;
  padding: 15px;
  border-radius: 4px;
  cursor: pointer;
  background-color: var(--bg-fileMessage);
  transition: all ease-in-out 0.3s;

  &:hover {
    .download {
      visibility: visible;
    }
  }

  .upload-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    color: white;
    font-size: 16px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-content: center;

    .progress-bar {
      width: 0;
      height: 4px;
      background-color: #409eff;
      transition: width 0.2s ease-in-out;
    }

    .upload-text {
      margin-top: 10px;
      font-size: 12px;
    }
  }

  .download {
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    background-color: rgba(0, 0, 0, 0.6);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    visibility: hidden;
  }
}
</style>
